package com.novellotus.iov.carvice.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RxMessageBean;
import com.novellotus.iov.carvice.interfaces.AdapterBottomInterface;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.StringBQUtils;
import com.novellotus.iov.carvice.utils.TimeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2017/7/27.
 *
 */

public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements AdapterBottomInterface {

    List<RxMessageBean.RecordsBean> recordsBeanList;
    final int BOTTOM = 100;
    private boolean showBottom = false;

    public MessageAdapter(List<RxMessageBean.RecordsBean> recordsBeanList) {
        this.recordsBeanList = recordsBeanList;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == recordsBeanList.size()) {
            return BOTTOM;
        } else {
            return 1;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == BOTTOM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_bottom, parent, false);
            return new BottomHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_message_item, parent, false);
            return new MessageHolder(view);
        }


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == recordsBeanList.size()) {
            initBottomHolder((BottomHolder) holder, position);
        } else {
            if (recordsBeanList.get(position) != null)
                initMessageHolder((MessageHolder) holder, position);
        }

    }

    @Override
    public int getItemCount() {
        if (showBottom) {
            return recordsBeanList.size() + 1;
        } else {
            return recordsBeanList.size();
        }
    }

    private void initBottomHolder(BottomHolder holder, int position) {
        // holder.rlt_bottom.setBackgroundResource(R.color.white);
    }

    private void initMessageHolder(MessageHolder holder, int position) {
        if (position == recordsBeanList.size() - 1) {
            holder.view_hide_bottom.setVisibility(View.INVISIBLE);
        } else {
            holder.view_hide_bottom.setVisibility(View.VISIBLE);
        }

        if (recordsBeanList.get(position).getReader().equals("0")) {
            holder.tv_company.setTextColor(Color.parseColor("#333333"));
            holder.tv_des.setTextColor(Color.parseColor("#333333"));
            holder.tv_time.setTextColor(Color.parseColor("#333333"));
        } else {
            holder.tv_company.setTextColor(Color.parseColor("#333333"));
            holder.tv_des.setTextColor(Color.parseColor("#888888"));
            holder.tv_time.setTextColor(Color.parseColor("#888888"));
        }
        if (recordsBeanList.get(position).getEname() == null || recordsBeanList.get(position).getEname().length() == 0) {
            holder.tv_company.setText("系统消息");
        } else {
            holder.tv_company.setText(recordsBeanList.get(position).getEname());
        }
        holder.tv_des.setText(StringBQUtils.ToDBC(recordsBeanList.get(position).getContent()));
        holder.tv_time.setText(TimeUtils.stampTotime(recordsBeanList.get(position).getCreateTime() + ""));
    }

    /**
     * 刷新数据
     */
    public void update(List<RxMessageBean.RecordsBean> recordsBeanList) {
        this.recordsBeanList = recordsBeanList;
        notifyDataSetChanged();
    }

    /**
     * 添加下拉时的
     */
    public void addData(List<RxMessageBean.RecordsBean> recordsBeanList) {
        this.recordsBeanList.addAll(recordsBeanList);
        notifyDataSetChanged();
    }

    @Override
    public void showBottomLayout() {
        showBottom = recordsBeanList.size() >= 5;
        notifyDataSetChanged();
    }

    @Override
    public void hideBottomLayout() {
        showBottom = false;
        notifyDataSetChanged();
    }

    class MessageHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_company)
        TextView tv_company;
        @BindView(R.id.tv_des)
        TextView tv_des;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.rlt_item)
        RelativeLayout rlt_item;
        @BindView(R.id.view_hide_bottom)
        View view_hide_bottom;

        MessageHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rlt_bottom)
        RelativeLayout rlt_bottom;

        BottomHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
